﻿namespace GooglePlusLib.NET
{
    using System;
    using System.IO;
    using System.Net;
    using System.Web.Script.Serialization;

    public class GooglePlusAPIHelper
    {
        private string _apiKey;
        private string _pageId;

        public GooglePlusAPIHelper(string pageId, string apiKey)
        {
            this._pageId = pageId;
            this._apiKey = apiKey;
        }

        public GPlusActivity GetActivity(string activityId)
        {
            string requestString = "https://www.googleapis.com/plus/v1/activities/" + activityId + "?key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<GPlusActivity>(webData);
        }

        public GPlusComment GetComment(string commentId)
        {
            string requestString = "https://www.googleapis.com/plus/v1/comments/" + commentId + "?key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<GPlusComment>(webData);
        }

        public GPlusPerson GetPerson()
        {
            string requestString = "https://www.googleapis.com/plus/v1/people/" + this._pageId + "?key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<GPlusPerson>(webData);
        }

        private string GetWebData(string requestString)
        {
            using (StreamReader reader = new StreamReader(WebRequest.Create(requestString).GetResponse().GetResponseStream()))
            {
                return reader.ReadToEnd().Replace("\"object\":", "\"plusObject\":");
            }
        }

        public GPlusActivities ListActivities()
        {
            return this.ListActivities("");
        }

        public GPlusActivities ListActivities(string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/people/" + this._pageId + "/activities/public?maxResults=20&pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<GPlusActivities>(webData);
        }

        public GPlusComments ListComments(string activityId)
        {
            return this.ListComments(activityId, "");
        }

        public GPlusComments ListComments(string activityId, string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/activities/" + activityId + "/comments?pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<GPlusComments>(webData);
        }

        public GPlusPeople ListPeopleByActivity(string activityId, PeopleActivityType activityType)
        {
            return this.ListPeopleByActivity(activityId, activityType, "");
        }

        public GPlusPeople ListPeopleByActivity(string activityId, PeopleActivityType activityType, string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/activities/" + activityId + "/people/" + activityType.ToString().ToLower() + "&pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<GPlusPeople>(webData);
        }

        public GPlusActivities SearchActivities(string query)
        {
            return this.SearchActivities(query, "");
        }

        public GPlusActivities SearchActivities(string query, string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/activities?query=" + query + "&pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<GPlusActivities>(webData);
        }

        public GPlusPeople SearchPeople(string query)
        {
            return this.SearchPeople(query, "");
        }

        public GPlusPeople SearchPeople(string query, string nextPageToken)
        {
            string requestString = "https://www.googleapis.com/plus/v1/people?query=" + query + "&pageToken=" + nextPageToken + "&key=" + this._apiKey;
            string webData = this.GetWebData(requestString);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<GPlusPeople>(webData);
        }
    }
}

